//index.js
import { getNewCoursesList, getHotCoursesList, getBestCoursesList, getRecommendCoursesList } from '../../api/courses.js'
import { getSliders, getCity } from '../../api/other.js'
import login from '../../utils/login'

Page({
  data: {
    city: '',
    sliders: [
      // {thumb: '',
      // url: ''}
    ],
    newCourses: [
      // {
      //   id: '1',
      //   title: '1',
      //   price: '1',
      //   priceOld: 200,
      //   thumb: 'https://img30.360buyimg.com/babel/s590x470_jfs/t1/163664/11/237/172437/5fedc026E18f95c63/f173a578ad46d5cb.jpg.webp'
      // },
    ],
    hotCourses: [
      // {
      //   id: '1',
      //   title: '课程名称1111',
      //   price: '99',
      //   priceOld: 200,
      //   buyCount: 155,
      //   thumb: 'https://img30.360buyimg.com/babel/s590x470_jfs/t1/163664/11/237/172437/5fedc026E18f95c63/f173a578ad46d5cb.jpg.webp'
      // }
    ],
    bestCourses: [
      // {
      //   id: '1',
      //   title: '课程名称1111',
      //   price: '99',
      //   priceOld: 200,
      //   buyCount: 155,
      //   thumb: 'https://img30.360buyimg.com/babel/s590x470_jfs/t1/163664/11/237/172437/5fedc026E18f95c63/f173a578ad46d5cb.jpg.webp'
      // }
    ],
    recommendCourses: [
      // {
      //   id: '1',
      //   title: '课程名称1111',
      //   price: '99',
      //   buyCount: 155,
      //   recommendedCount: 66,
      //   thumb: 'https://img30.360buyimg.com/babel/s590x470_jfs/t1/163664/11/237/172437/5fedc026E18f95c63/f173a578ad46d5cb.jpg.webp'
      // }
    ]
  },

  indexServicePageTo(name) {
    console.log(name)
  },
  goCoursesSearchPages() {
    // wx.navigateTo({
    //   url: '/pages/search-courses/search-courses'
    // })
    wx.navigateTo({
      url: '/pages/search-courses/search-courses-mini'
    })
  },
  goCreativityTestPage() { },
  bindtapSwiper(e) {
    wx.redirectTo({
      url: '/pages/web/web?url=' + this.data.sliders[e.currentTarget.dataset.index].url,
    })
  },
  initLocation() {
    // 获取位置信息
    wx.getLocation({
      type: 'wgs84',
      success: res => {
        getCity({
          longitude: res.latitude,
          latitude: res.longitude
        }).then(res => {
          this.setData({
            city: res.city
          })
        })
      }
    })
  },

  async onLoad() {
    // 处理登录
    login()

    getSliders().then(data => {
      let sliders = data.map(item => ({
        url: item.url,
        thumb: item.thumb
      }))
      this.setData({
        sliders
      })
    })
    getNewCoursesList().then(({ data }) => {
      let newCourses = data.map(item => ({
        id: item.id,
        title: item.title,
        price: item.charge,
        priceOld: item.charge * 1.2,
        thumb: item.thumb
      }))
      this.setData({
        newCourses
      })
    })
    getHotCoursesList().then(({ data }) => {
      let hotCourses = data.map(item => ({
        id: item.id,
        title: item.title,
        price: item.charge,
        priceOld: item.charge * 1.2,
        buyCount: item.user_count,
        thumb: item.thumb
      }))
      this.setData({
        hotCourses
      })
    })
    getBestCoursesList().then(({ data }) => {
      let bestCourses = data.map(item => ({
        id: item.id,
        title: item.title,
        price: item.charge,
        priceOld: item.charge * 1.2,
        buyCount: item.user_count,
        thumb: item.thumb
      }))
      this.setData({
        bestCourses
      })
    })
    getRecommendCoursesList().then(({ data }) => {
      let recommendCourses = data.map(item => ({
        id: item.id,
        title: item.title,
        price: item.charge,
        priceOld: item.charge * 1.2,
        recommendedCount: item.is_rec,
        thumb: item.thumb
      }))
      this.setData({
        recommendCourses
      })
    })
    this.initLocation()
  },
  /**
 * 生命周期函数--监听页面显示
 */
  onShow: function () {
    if (typeof this.getTabBar === 'function' && this.getTabBar()) {
      this.getTabBar().setData({
        active: 'index'
      })
    }
  }
})
